package controllers

import (
	"fly/helper"
	"fly/models"
	"fly/req"
	"fly/resp"
	"fly/service"
	"github.com/beego/beego/v2/client/orm"
	"github.com/duke-git/lancet/v2/convertor"
	"github.com/duke-git/lancet/v2/slice"
)

// SysMenuController 系统菜单
type SysMenuController struct {
	BaseController
	SysMenuService service.SysMenuService
	SysUserService service.SysUserService
}

// AddSysMenu ...
// @Title 添加菜单
// @Description 添加一个系统配置-菜单
// @Param token header  string	false "授权Token"
// @Param body body req.AddMenuReq true "添加菜单数据"
// @Success 200 {object} resp.SysMenuResp
// @router /save [post]
func (c *SysMenuController) AddSysMenu() {
	con := req.AddMenuReq{}
	c.CheckReqErrorAbort(c.BindJSON(&con))
	c.ValidReqParams(&con)
	mSysMenu := models.SysMenu{}
	c.CheckRespErrorAbort(helper.CopyDeep(&mSysMenu, con))
	id := con.Id
	var err error
	if con.Id > 0 {
		old, _ := models.GetSysMenuById(con.Id)
		c.CheckReqErrorAbort(helper.RequireTrue(old != nil, "未找到资源"))
		_ = helper.CopyOut(old, con)
		err = models.UpdateSysMenuById(old, c.GetReqUserId())
	} else {
		id, err = models.AddSysMenu(&mSysMenu, c.GetReqUserId())
	}
	c.CheckRespErrorAbort(err)
	r := resp.SysMenuResp{}
	data, err := models.GetSysMenuById(id)
	_ = resp.Copy2TimeResp(&r, data)
	c.Success(r)
}

// All ...
// @Title 查询菜单
// @Description 查询所有菜单列表
// @Param token header  string	false "授权Token"
// @Success 200 {object} []resp.SysMenuResp
// @router /list [get]
func (c *SysMenuController) All() {
	menuType, _ := c.GetInt("type", -1)
	var list []*models.SysMenu
	qs := orm.NewOrm().QueryTable(new(models.SysMenu)).OrderBy("sort")
	if menuType >= 0 {
		qs = qs.Filter("menu_type", menuType)
	}
	_, err := qs.All(&list)
	c.CheckRespErrorAbort(err)
	var results []resp.SysMenuResp
	c.CheckRespErrorAbort(resp.Copy2TimeResp(&results, &list))
	_ = slice.SortByField(results, "Sort", "asc")
	c.Success(resp.MenuList2Tree(results))
}

// Nav ...
// @Title 当前用户菜单
// @Description 查询当前用户能访问的菜单列表
// @Param token header  string	false "授权Token"
// @Success 200 {object} []resp.SysMenuResp
// @router /nav [get]
func (c *SysMenuController) Nav() {
	user, err := c.SysUserService.GetUserById(c.GetReqUserId())
	c.CheckRespErrorAbort(err)
	menuType, _ := c.GetInt("type", 0)
	var list []*models.SysMenu
	if user.IsSuperAdmin() {
		// 最高权限查询所有
		_, err = orm.NewOrm().QueryTable(new(models.SysMenu)).Filter("menu_type", 0).OrderBy("sort").All(&list)
	} else {
		list, err = c.SysMenuService.GetUserMenuList(user.Id, &menuType)
	}
	var results []resp.SysMenuResp
	c.CheckRespErrorAbort(resp.Copy2TimeResp(&results, &list))
	c.Success(resp.MenuList2Tree(results))
}

// Del ...
// @Title 删除菜单
// @Description 查询所有菜单列表
// @Param token header  string	false "授权Token"
// @Param path id int true "菜单id"
// @Success 200 {int} "id"
// @router /:id [delete]
func (c *SysMenuController) Del() {
	id, err := convertor.ToInt(c.Ctx.Input.Param(":id"))
	c.CheckReqErrorAbort(err)
	c.CheckReqErrorAbort(helper.RequireTrue(c.SysUserService.IsSuperAdminUser(c.GetReqUserId()), "超级管理员才能操作"))
	c.CheckRespErrorAbort(models.DeleteSysMenu(id))
	// 删除角色菜单关系
	_, err = orm.NewOrm().QueryTable(new(models.SysRoleMenu)).Filter("menu_id", id).Delete()
	c.Success(id)
}
